﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YJYpublicClass;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace YJYpublicClass
{
    public partial class Frm_海康相机2 : Form
    {
        HKCamera2 tempdev = null;
        public Frm_海康相机2(HKCamera2 obj)
        {
            InitializeComponent();
            tempdev=obj;
        }

        private void Frm_海康相机2_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = tempdev;
            timer1.Enabled = true;
          //  tempdev.PictureBox1 =pictureBox1;
        }
 
        //public Frm_相机设置(CameraDSscan obj)
        //{
        //    InitializeComponent();
        //    tempdev = obj;
        //    foreach (var item in CameraDSscan.CameraSnNames)
        //    {
        //        comboBox1.Items.Add(item);
        //    }

        //}

        private void btn_保存设置_Click(object sender, EventArgs e)
        {
         
            if (tempdev.SaveConfig())
            {
                MessageBox.Show("数据保存成功");
            }
            else
            {
                //保存失败
                MessageBox.Show("数据保存失败");
            }
        }

        private void btn_设置曝光_Click(object sender, EventArgs e)
        {
            try
            {
                if (tempdev.SetExposuretime(tempdev.config.Exposuretime))
                {
                    MessageBox.Show("曝光设置成功");
                }
                else
                {
                    MessageBox.Show("曝光设置失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("曝光设置失败:" + ex.Message);
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // this.imageBox1.Image = tempdev.Image;
            //  PublicFuntions.DispImage(tempdev.Image, hWindowControl1.HalconWindow);
            if(tempdev.OutVisionImage!=null)
            {
                try
                {
                  //  pictureBox1.Image =((Image<Bgr, byte>)tempdev.tempreimg).ToBitmap();
                    this.cogRecordDisplay1.Image = tempdev.OutVisionImage;
                    cogRecordDisplay1.Fit();
                    //
                }
                catch
                { 
                }
               
            }
            
            if (cbx_更新.Checked)
            {
                this.propertyGrid1.SelectedObject = tempdev;
            }
            lab_zhenlv.Text = tempdev.zhenlv.ToString("0.00");
            lab_耗时.Text = tempdev.haoshi.ToString();
        }

        private void btn_打开相机_Click(object sender, EventArgs e)
        {
            try
            {
                tempdev.OpenCamerabyname(tempdev.config.CameraSNname);
                richTextBox1.AppendText("完成" + "\r\n");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_关机相机_Click(object sender, EventArgs e)
        {
            try
            {
                tempdev.CloseCamera();
                richTextBox1.AppendText("完成" + "\r\n");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_开始取图_Click(object sender, EventArgs e)
        {
            tempdev.StartGrab();
            richTextBox1.AppendText("完成" + "\r\n");
        }

        private void btn_停止取图_Click(object sender, EventArgs e)
        {
            tempdev.StopGrab();
            richTextBox1.AppendText("完成" + "\r\n");
        }

        private void btn_查询_Click(object sender, EventArgs e)
        {
            HKCamera2.Enumdevlist();
            comboBox1.Items.Clear();
            foreach (var item in HKCamera2.CameraSnNames)
            {
                comboBox1.Items.Add(item);
            }
            richTextBox1.AppendText("查询完成" + "\r\n");
        }

        private void btn_软触发_Click(object sender, EventArgs e)
        {
            if(!tempdev.SoftTrige())
            {
                richTextBox1.AppendText("触发失败");
            }
        }
    }
}
